package com.yueyang.sojourn.server.common.baymax.web.filter;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;

/* non-public */ final class DES {
    private static final String DES_ALGORITHM = "DES";

    private DES() {
        super();
    }

    /**
     * DES对字符串解密，默认字符编码为utf-8
     *
     * @param input     待解密字符串
     * @param secretKey 密钥字符串
     * @return 返回DES解密后的字节数组
     * @since: 1.0.10
     * @see #decrypt(byte[], byte[])
     */
    public static byte[] decrypt(String input, String secretKey) {
        try {
            return decrypt(input.getBytes(StandardCharsets.UTF_8.name()),
                    secretKey.getBytes(StandardCharsets.UTF_8.name()));
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }


    /**
     * DES解密数据
     *
     * @param input     待解密的字节数组
     * @param secretKey 密钥字节数组
     * @return 返回DES解密后的数据，如果 input ==null or secretKey==null,或JCE没安装正确，则抛出IllegalArgumentException
     * @since: 1.0.10
     */
    public static byte[] decrypt(byte[] input, byte[] secretKey) {
        if (input == null) {
            throw new IllegalArgumentException("加密字节数组为null");
        }
        if (secretKey == null) {
            throw new IllegalArgumentException("密钥字节数组为null");
        }
        try {
            Cipher cipher = Cipher.getInstance("DES");
            DESKeySpec dks = new DESKeySpec(secretKey);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, keyFactory.generateSecret(dks));
            return cipher.doFinal(input);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * DES对字符串加密
     *
     * @param input     待加密字符串
     * @param secretKey 密钥字符串
     * @return 返回DES加密后的字节数组
     * @since: 1.0.10
     * @see #encrypt(byte[], byte[])
     */
    public static byte[] encrypt(String input, String secretKey) {
        try {
            return encrypt(input.getBytes(StandardCharsets.UTF_8.name()),
                    secretKey.getBytes(StandardCharsets.UTF_8.name()));
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * DES加密数据
     *
     * @param input     待加密的字节数组
     * @param secretKey 密钥字节数组
     * @return 返回DES加密后的数据，如果 input ==null or secretKey==null,或JCE没安装正确，则抛出IllegalArgumentException
     * @since: 1.0.10
     */
    public static byte[] encrypt(byte[] input, byte[] secretKey) {
        if (input == null) {
            throw new IllegalArgumentException("加密字节数组为null");
        }
        if (secretKey == null) {
            throw new IllegalArgumentException("密钥字节数组为null");
        }
        try {
            Cipher cipher = Cipher.getInstance(DES_ALGORITHM);
            DESKeySpec dks = new DESKeySpec(secretKey);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generateSecret(dks));
            return cipher.doFinal(input);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
}


